
#include "WTSXSipService/SipServiceUtils.hpp"
#include "WTResipDll/SipWtsipUtils.hpp"

#include "resip/stack/SipMessage.hxx"
#include "resip/stack/PlainContents.hxx"

namespace wtsxsip{

CSipServiceUtils::CSipServiceUtils(void)
{
}

CSipServiceUtils::~CSipServiceUtils(void)
{
}


bool CSipServiceUtils::setBody4Message(wtsip::CSipMessage& msg, const SMsgBodyBase& body)
{
    if (body.type.empty() || body.subType.empty() || body.contents.empty()){
        return false;
    }
    
    resip::Data _Body(body.contents.c_str(), body.contents.size());
    resip::Data _Type(body.type.c_str(), body.type.size());
    resip::Data _SubType(body.subType.c_str(), body.subType.size());
    resip::Contents* cBody = new resip::PlainContents(_Body, resip::Mime(_Type, _SubType));
    msg.setContents(std::auto_ptr<resip::Contents>(cBody));

    return true;
}


bool CSipServiceUtils::getHeadersFromExternal(SHeaders& headers, wtsip::CSipMessage& msg)
{
    if (msg.isRequest())
    {
        headers.eMethod = wtsip::CSipWtsipUntils::tran2WtMethod(msg.method());
        wtsip::CSipWtsipUntils::tran2SUri(headers.sLocal, msg.header(resip::h_To));
        wtsip::CSipWtsipUntils::tran2SUri(headers.sRemote, msg.header(resip::h_From));
    } else if (msg.isResponse())
    {
        int code = msg.header(resip::h_StatusLine).responseCode();
        headers.eCode = wtsip::CSipWtsipUntils::getTypeByCode(code);
        wtsip::CSipWtsipUntils::tran2SUri(headers.sRemote, msg.header(resip::h_To));
        wtsip::CSipWtsipUntils::tran2SUri(headers.sLocal, msg.header(resip::h_From));
    }
    headers.iSeq = msg.header(resip::h_CSeq).sequence();
    if (msg.exists(resip::h_Contacts)){
        wtsip::CSipWtsipUntils::tran2SUri(headers.sRContact, msg.header(resip::h_Contacts).front());
    }
    if (msg.exists(resip::h_Expires)){
        headers.iExpire = msg.header(resip::h_Expires).value();
    }
    return true;
}


bool CSipServiceUtils::copy(SHeaders& dst, const SHeaders& src)
{
    //Request
    copy(dst.sTar, src.sTar);

    //Response
    dst.eCode = src.eCode;
    dst.szReason = src.szReason;

    //Shared
    dst.eMethod = src.eMethod;
    dst.iExpire = src.iExpire;
    dst.iSeq = src.iSeq;
    copy(dst.sRemote, src.sRemote);
    copy(dst.sLocal, dst.sLocal);
    copy(dst.sRContact, dst.sRContact);
    copy(dst.sLContact, dst.sLContact);
    return true;
}


bool CSipServiceUtils::copy(wtsip::SUri& dst, const wtsip::SUri& src)
{
    dst.szName = src.szName;
    dst.szID = src.szID;
    dst.szIP = src.szIP;
    dst.iPort = src.iPort;
    return true;
}


resip::TransportType CSipServiceUtils::getResipType(const ETransport type)
{
    resip::TransportType rType;
    switch(type)
    {
    case wtsxsip::UDP:
        rType = resip::UDP;
        break;
    case wtsxsip::TCP:
        rType = resip::TCP;
        break;
    case wtsxsip::TLS:
        rType = resip::TLS;
        break;
    default:
        rType = resip::UDP;
        break;
    }
    return rType;
}


}//namespace
